<?php
namespace app\service;

use app\service\orderService;
use think\Db;
class chargeRuleService
{
	public function carvaluemoney($in_time,$out_time,$price){
		$stime=$out_time-$in_time;
		$data=$this->gettime2($stime);
		$days=$data["days"];
		
		if($data["hours"]!="0")
		{
			$days=$days+1;
		}
		$money=$days*$price;
		if($money=="0"){
			$money=$price;
		}
		return $money;
	}
	
	public function getmoney($out_parking_id,$in_time,$out_time){
		$parkinglotinfo=Db::table("parkinglotinfo")->where("out_parking_id",$out_parking_id)->find();
		$arr["errcode"]="0";
		$money=0;
		if(empty($parkinglotinfo)){
			$arr["errcode"]="1";
		}
		$ruleinfo=Db::table("chargerule")->where("id",$parkinglotinfo["chargerule_id"])->find();
		if(empty($ruleinfo)){
			$arr["errcode"]="1";
		}
		//获取停车分钟数
		$min=$this->getmin($in_time,$out_time);
		if($ruleinfo["type"]=="2"){
			//按时收费
			if($min<=$ruleinfo["freetime"]){
				$money=0;
			}else{
				$stime=$out_time-$in_time-$ruleinfo["freetime"]*60;
				$data=$this->gettime2($stime);
				$money=$data["hours"]*$ruleinfo["price"];
				if($money >= $ruleinfo['maxmoney']){
					$money= $ruleinfo['maxmoney'];
				}
				$money=$money+$ruleinfo['maxmoney']*$data["days"];
			}
		}elseif($ruleinfo["type"]=="3"){
			//按次收费
			if($min<=$ruleinfo["freetime"]){
				$money=0;
			}else{
				$stime=$out_time-$in_time-$ruleinfo["freetime"]*60;
				$data=$this->gettime2($stime);
				$days=$data["days"];
				$money = $ruleinfo['price']*($days+1);
			}
			
		}elseif($ruleinfo["type"]=="4"){
			//按时收费(加上免费时长)
			if($min<=$ruleinfo["freetime"]){
				$money=0;
			}else{
				$stime=$out_time-$in_time;
				$data=$this->gettime2($stime);
				$money=$data["hours"]*$ruleinfo["price"];
				if($money >= $ruleinfo['maxmoney']){
					$money= $ruleinfo['maxmoney'];
				}
				$money=$money+$ruleinfo['maxmoney']*$data["days"];
			}
			
		}else{
			$arr["errcode"]="1";
		}
		
		$arr["money"]=$money;
		return $arr;
    }
	
	
    
	
	public function getmin($in_time,$out_time){
		$minute=floor(($out_time-$in_time)/60);
		return $minute;
	}
	public function getmin_old($in_time,$out_time){
		$minute=floor((strtotime($out_time)-strtotime($in_time))/60);
		return $minute;
	}
	
	
	
	//停车时间
	public function gettime($in_time,$out_time){
		$days=floor(($out_time-$in_time)/86400);
        $hours=floor(($out_time-$in_time)%86400/3600);
        $min=floor(($out_time-$in_time)%86400/60)-$hours*60;
		$str="";
		if($days!="0"){
			$str=$str.$days."天";
		}
		if($hours!="0"){
			$str=$str.$hours."时";
		}
		if($min!="0"){
			$str=$str.$min."分";
		}
		return $str;
	}
	public function gettime_old($in_time,$out_time){
		$days=floor((strtotime($out_time)-strtotime($in_time))/86400);
        $hours=floor((strtotime($out_time)-strtotime($in_time))%86400/3600);
        $min=floor((strtotime($out_time)-strtotime($in_time))%86400/60)-$hours*60;
		$str="";
		if($days!="0"){
			$str=$str.$days."天";
		}
		if($hours!="0"){
			$str=$str.$hours."时";
		}
		if($min!="0"){
			$str=$str.$min."分";
		}
		return $str;
	}
	
	public function gettime2($time){
		$arr["days"]=floor(($time)/86400);
        $arr["hours"]=floor(($time)%86400/3600);
        $arr["min"]=floor(($time)%86400/60)-$arr["hours"]*60;
		if($arr["min"]>0){
			$arr["hours"]=$arr["hours"]+1;
		}
		return $arr;
	}
	
	public function getmoney2($out_parking_id,$in_time,$out_time){
		$standardinfo = (new orderService())->standardInfo($out_parking_id);
		$in_duration = (strtotime($out_time)-strtotime($in_time))/3600-$standardinfo['exempting'];
		$money=0;
		$arr["errcode"]="0";
		if($in_duration > 0){
			if($standardinfo['standard_type'] == 1){//按次收费
				$money = $standardinfo['money'];
			}elseif($standardinfo['standard_type'] == 2){//按时收费
				//计算停车天数
				$number = $in_duration/24;
				if($number <= 1){
					//24小时之内，减去免费时长，计算收费
					$money = $standardinfo['money'] * ceil($in_duration);
					//判断是否超过最大收费
					if($money >= $standardinfo['maxmoney']){
						$money= $standardinfo['maxmoney'];
					}
				}else{
					//超过24小时
					$money = $standardinfo['money'] * (ceil($in_duration)%24);
					//判断是否超过最大收费
					if($money >= $standardinfo['maxmoney']){
						$money = $standardinfo['maxmoney'];
					}
					$money = $money + ($standardinfo['maxmoney'] * floor($number));
				}
			}else{
				$arr["errcode"]="1";
			}
		}else{
			$money = 0;
		}
		$arr["money"]=$money;
		return $arr;
    }
	
}